
/** streams.d: The unit test for dotmars/io/*stream.d

  Written in the D programming language 1.0

Authors:	Wei Li (oldrev@gmail.com)
Copyright:	Copyright (C) 2007 by Wei Li.
License:	BSD
*/

module test.io.teststreams;

import dotmars.test.core;

import dotmars.io.bufferedstream;
import dotmars.io.memorystream;


class MemoryStreamTestCase : TestCase!(MemoryStreamTestCase)
{

	this()
	{
		register("testMemoryStream", &testMemoryStream);
	}

	void testMemoryStream() 
	{
		scope auto ms = new MemoryStream(4096);
		const ubyte[10] s1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
		ubyte s2[10];
		ms.write(s1);
		assertTrue(ms.position == s1.length);
		ms.position = 0;
		size_t n = ms.read(s2);
		assertTrue(ms.position == s1.length);
		assertTrue(n == s1.length);
		assertTrue(s1[] == s2[]);
	}
}

class BufferedStreamTestCase : TestCase!(BufferedStreamTestCase)
{

	this()
	{
		register("testBufferedStream", &testBufferedStream);
	}

	void testBufferedStream() 
	{
		scope auto ms = new MemoryStream(4096);
		const ubyte[10] s1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
		ubyte s2[10];
		ms.write(s1);

		auto bs = new BufferedStream(ms);
		bs.write(s1);
		assertTrue(bs.length == s1.length * 2);
		assertTrue(bs.position == s1.length * 2);

		bs.position = 0;
		size_t n = bs.read(s2);
		assertTrue(n == s2.length);
		assertTrue(bs.position == n);
		assertTrue(s1[] == s2[]);
	}
}

class StreamsTestSuite : TestSuite!(StreamsTestSuite)
{
	this() 
	{
		register(new MemoryStreamTestCase, new BufferedStreamTestCase);
	}
}
